package com.google.demo.web;

import com.google.demo.R;
import com.google.demo.Util.Util;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MyWebView extends Activity{
	private MyWebView me=this;
	private WebView mWebView;
	private String mData;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		me.getWindow().requestFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.webview_layout);	
		me.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		mWebView=(WebView) findViewById(R.id.webView1_website);
		Bundle bundle=this.getIntent().getExtras();
		mData=bundle.getString("webdata");	
		configure();
		setResult(RESULT_OK);
	}
	private void configure(){
		WebSettings mWebSettings=mWebView.getSettings();
		String ua=Util.getUserAgentString(getApplicationContext());		
		mWebSettings.setJavaScriptEnabled(true);
		mWebSettings.setBuiltInZoomControls(true);
		mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		mWebSettings.setUserAgentString(ua);
		mWebView.setWebChromeClient(new WebChrome());	
		mWebView.setWebViewClient(new WebClient());	
		mWebView.loadUrl(mData);
	}
	private class WebClient extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			view.loadUrl(url);
			return super.shouldOverrideUrlLoading(view, url);
		}
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			
			super.onPageStarted(view, url, favicon);
		}
		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			
			super.onPageFinished(view, url);
		}
		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			// TODO Auto-generated method stub
			super.onReceivedError(view, errorCode, description, failingUrl);
			
		}
	}
	private class WebChrome extends WebChromeClient{
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			// TODO Auto-generated method stub
			me.setTitle(me.getString(R.string.please_wait));
			me.setProgress(newProgress * 100);
			if (newProgress == 100)
				me.setTitle(me.getString(R.string.please_wait));
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Util.clearCookieWeb(this);
	}
}
